package com.haoshuang.zhouzhoubang.ui.fragment

import android.os.Bundle
import com.haoshuang.zhouzhoubang.R
import com.haoshuang.zhouzhoubang.databinding.FragmentAppointmentOrderBinding
import com.haoshuang.zhouzhoubang.model.bean.OrderBean
import com.haoshuang.zhouzhoubang.ui.adapter.AppointmentOrderAdapter
import com.haoshuang.zhouzhoubang.ui.dialog.ShowOrderPriceDialog
import com.haoshuang.zhouzhoubang.ui.vm.AppointmentOrderViewModel
import com.lxj.xpopup.XPopup
import com.mufeng.libs.base.BaseVMFragment
import com.mufeng.libs.core.setEmptyData
import com.mufeng.libs.core.setOnItemChildClick
import com.mufeng.libs.core.setOnItemClick
import com.mufeng.libs.core.setupData
import com.mufeng.libs.core.setupEmptyData
import com.mufeng.libs.state.observeState
import com.mufeng.libs.utils.toast
import com.mufeng.libs.utils.view.vertical


/**
 * 预约客户订单列表
 */
class AppointmentOrderFragment :
    BaseVMFragment<AppointmentOrderViewModel, FragmentAppointmentOrderBinding>() {

    companion object {
        fun getInstance(): AppointmentOrderFragment {
            val fragment = AppointmentOrderFragment()
            fragment.arguments = Bundle().apply {

            }
            return fragment
        }
    }

    private val data = arrayListOf<OrderBean>()
    private val adapter = AppointmentOrderAdapter(data)
    fun refreshData(){
        binding.refreshLayout.autoRefresh()
    }
    override fun initView(savedInstanceState: Bundle?) {

        binding.recyclerView.vertical().adapter = adapter
        adapter.setEmptyData(R.mipmap.icon_empty_order, "暂无待预约订单")

        binding.refreshLayout.setOnRefreshListener {
            p = 1
            isShowing = false
            vm.getMasterAppointmentOrderList(p)
        }

        binding.refreshLayout.setOnLoadMoreListener {
            p ++
            isShowing = false
            vm.getMasterAppointmentOrderList(p)
        }

        adapter.setOnItemClick { view, position,_ ->

        }
        adapter.setOnItemChildClick(id = R.id.tvRemindCustomer){view, position, item ->
            if (item == null) return@setOnItemChildClick
            isShowing = true
            vm.remindCheck(item.id.toString())
        }
        adapter.setOnItemChildClick(id = R.id.payPrice){view, position, item ->
            if (item == null) return@setOnItemChildClick
            val teamPrice = item.team_price?.team_price
            val teamLeaderPrice = item.team_price?.team_leader_price
            val teamMemberPrice = item.team_price?.team_member_price
            val minePrice = item.team_price?.self_price
            XPopup.Builder(mActivity)
                .asCustom(
                    ShowOrderPriceDialog(
                        context = mActivity,
                        prepayments_amount = item.prepayments_amount ?: "0.0",
                        nightPrice = item.nighttime_amount ?: "0.0",
                        totalPrice = item.pay_price,
                        teamPrice = teamPrice ?: "",
                        teamLeaderPrice = teamLeaderPrice ?: "",
                        teamMemberPrice = teamMemberPrice ?: item.pay_price,
                        minePrice = minePrice ?: item.pay_price,
                    )
                )
                .show()
        }
    }

    override fun initData() {

    }

    override fun lazyLoadData() {

    }

    override fun onResume() {
        super.onResume()
        binding.refreshLayout.autoRefresh()
    }

    override fun startObserve() {

        vm.getMasterAppointmentOrderListData.observeState(this){
            onLoading {  }
            onError { code, msg ->
                dismissLoading()
                toast(msg)
                binding.refreshLayout.setupEmptyData(data, adapter, p)
            }
            onSuccess {
                dismissLoading()
                binding.refreshLayout.setupData(it, data, adapter, p)
            }
        }

        vm.remindCheckData.observeState(this){
            onLoading { showLoading() }
            onError { code, msg ->
                dismissLoading()
                toast(msg)
            }
            onSuccess {
                dismissLoading()
                toast("提醒成功")
            }
        }

    }

}